<?php

/**
 * Date: 2015-09-09
 */

namespace Admin\Controller;

use Think\AjaxPage;
use Think\Page;

class ZmdController extends BaseController {

    // 未审报单中心申请列表
    public function index() {
        $this->display();
    }

    public function ajaxindex() {
         $condition = array();
        I('account') ? $condition['account'] = I('account') : false;
        I('email') ? $condition['email'] = I('email') : false;
        I('mobile') ? $condition['mobile'] = I('mobile') : false;
        

        $model = M('bd_order');
        $count = $model->where($condition)->count();
        $Page = new AjaxPage($count, 10);
        //  搜索条件下 分页赋值
        foreach ($condition as $key => $val) {
            $Page->parameter[$key] = urlencode($val);
        }
        $userList = $model->where($condition)->order($sort_order)->limit($Page->firstRow . ',' . $Page->listRows)->select();
        
        $show = $Page->show();
        $this->assign('userList', $userList);
        $this->assign('page', $show); // 赋值分页输出
        $this->display();
    }

    //确认审核报单中心
    public function qrzmdsheqi($id) {
        $account = D('user_zmd')->where('id=' . $id)->field('u_id')->find(); //查出申请者编号
        $this->account = $account;
        $res = D('user_zmd')->where('id=' . $id)->save(array('type' => 2)); //确认审批申请
        if ($res) {
            M('users')->where(array('user_id' => $account['u_id']))->save(array('zmd' => 3,'grade_id' =>'1'));
            $this->success("审批成功!");
        } else {
            $this->error("审批失败 请重新操作!");
        }
    }

    // 删除当前申请
    public function zmdsheqidel() {
        $account = D('user_zmd')->where('id=' . I('id'))->field('u_id')->find(); //查出申请者编号
        M('users')->where(array('user_id' => $account['u_id']))->save(array('zmd' => 1,'grade_id' =>'0'));
        $res = M('user_zmd')->where('id=' . I('id'))->delete();
        if ($res) {
            $this->success("删除成功");
        } else {
            $this->error("删除失败");
        }
    }

    // 己审报单中心列表
    public function zmdlist() {
        $this->display();
    }

    // 己审报单中心列表
    public function ajaxzmdlist() {
        $condition = array();
        I('mobile') ? $condition['mobile'] = I('mobile') : false;
        I('email') ? $condition['email'] = I('email') : false;
        I('account') ? $condition['account'] = I('account') : false;
        $condition['zmd'] = 3;
        $sort_order = I('order_by', 'user_id') . ' ' . I('sort', 'desc');
        

        $model = M('users');
        $count = $model->where($condition)->count();
        $Page = new AjaxPage($count, 10);
        //  搜索条件下 分页赋值
        foreach ($condition as $key => $val) {
            $Page->parameter[$key] = urlencode($val);
        }

        $userList = $model->where($condition)->order($sort_order)->limit($Page->firstRow . ',' . $Page->listRows)->select();
//        foreach ($userList as &$v) {
//            $grade = M('users')->where(array('zmd_id' => $v['user_id']))->sum('money');
//            $v['zmd_money'] = $grade;
//        }
        $show = $Page->show();
        $this->assign('userList', $userList);
        $this->assign('page', $show); // 赋值分页输出
        $this->display();
    }

    // 导出 表格
    public function export_zmd_user() {
        $sql = "select * from __PREFIX__users where zmd = 3 order by user_id desc";
        $orderList = D()->query($sql);
        $strTable = '<table width="500" border="1">';
        $strTable .= '<tr>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:120px;">序号</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:120px;">登录账号</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:120px;">呢称</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:120px;">姓名</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;width="120px">联系电话</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;width="120px">QQ</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;width="120px">微信</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;width="120px">邮箱</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;width="120px">支付宝</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:120px;">报单时间</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;width="120px">报单金额</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;width="120px">累积投资金额</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;width="120px">累积报单业绩</td>';
        $strTable .= '</tr>';
        if (is_array($orderList)) {
            foreach ($orderList as $k => $val) {
                $strTable .= '<tr>';
                $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;' . $val['user_id'] . '</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;' . $val['account'] . '</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;' . $val['username'] . '</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;' . $val['truename'] . '</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;' . $val['phone'] . '</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;' . $val['qq'] . '</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;' . $val['weixin'] . '</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;' . $val['email'] . '</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;' . $val['zfb'] . '</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;' . $val['regtime'] . '</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;' . $val['money'] . '</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;' . $val['money_total'] . '</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;' . M('users')->where(array('zmd_number' => $val['account']))->sum('money') . '</td>';
                $strTable .= '</tr>';
            }
        }
        $strTable .='</table>';
        unset($orderList);
        downloadExcel($strTable, '报单中心列表');
        exit();
    }

    // 报单中心所属会员
    public function zmdhuiyuan() {
        $user_id = I('get.id');
        $model = M('users');
        $userList = $model->where(array('zmd_id' => $user_id))->select();
        foreach ($userList as &$v) {
            $grade = M('user_level')->where(array('level_id' => $v['level']))->field('level_name')->find();
            $v['level_name'] = $grade['level_name'];
        }
        $user_id_arr = get_arr_column($userList, 'user_id');
        $this->assign('userList', $userList);
        $this->display();
    }

    // 添加服务中心  
    public function addzmd() {
        $user_id = I('user_id', '');
        if ($user_id == '') {
            die('<script>alert("操作失败，请刷新页面后重试");history.go(-1);</script>');
        } else {
            $res = M('users')->where('user_id=' . $user_id)->save(array('zmd' => 3,'grade_id' =>1));
            if ($res) {
                $this->success("设置成功");
            } else {
                $this->error("设置失败");
            }
        }
    }

    // 取消报单中心 
    public function addzmddel() {
        $user_id = I('user_id', '');
        if ($user_id == '') {
            die('<script>alert("操作失败，请刷新页面后重试");history.go(-1);</script>');
        } else {
            $res = M('users')->where('user_id=' . $user_id)->save(array('zmd' => 1,'grade_id' =>'0'));
            if ($res) {
                $this->success("取消成功");
            } else {
                $this->error("取消失败");
            }
        }
    }

    // 报单中心级别开始
    public function grade() {
        $act = I('GET.act', 'add');
        $this->assign('act', $act);
        $grade_id = I('GET.grade_id');
        $grade_info = array();
        if ($grade_id) {
            $grade_info = D('user_grade')->where('grade_id=' . $grade_id)->find();
            $this->lever = M('user_level')->field('level_id,level_name,amount')->select();
            $this->assign('info', $grade_info);
        }
        $this->display();
    }

    public function gradeList() {
        $Ad = M('user_grade');
        $res = $Ad->where('1=1')->order('grade_id')->page($_GET['p'] . ',10')->select();
        if ($res) {
            foreach ($res as &$val) {
                $level = M('user_level')->where(array('level_id' => $val['level_id']))->field('level_name')->find();
                $val['level_name'] = $level['level_name'];
                $list[] = $val;
            }
        }
        $this->assign('list', $list);
        $count = $Ad->where('1=1')->count();
        $Page = new \Think\Page($count, 10);
        $show = $Page->show();
        $this->assign('page', $show);
        $this->display();
    }

    public function gradeHandle() {
        $data = I('post.');
        if ($data['act'] == 'add') {
            $r = D('user_grade')->add($data);
        }

        if ($data['act'] == 'edit') {
            $r = D('user_grade')->where('grade_id=' . $data['grade_id'])->save($data);
        }

        if ($data['act'] == 'del') {
            $r = D('user_grade')->where('grade_id=' . $data['grade_id'])->delete();
            if ($r)
                exit(json_encode(1));
        }

        if ($r) {
            $this->success("操作成功", U('Admin/Zmd/gradeList'));
        } else {
            $this->error("操作失败", U('Admin/Zmd/gradeList'));
        }
    }

    // 报单中心级别结束
    
    /**
     * 空单实单操作开始 ===============================================华丽的分界线
     */
    // 设为空单
    public function hollow_user() {
        $user_id = I('user_id', '');
        if ($user_id == '') {
            die('<script>alert("操作失败，请刷新页面后重试");history.go(-1);</script>');
        } else {
            $user = M('users')->where(array('user_id' => $user_id))->field('user_id,nickname,level')->find();
            $level = M('user_level')->where(array('level_id' => $user['level']))->find(); //会员等级查询

            $res = M('users')->where('user_id=' . $user_id)->save(array('is_user' => 0, 'is_activate' => 1, 'jh_time' => time()));
            if ($res) {
                activate_log($user_id, 2, '管理员' . $_SESSION['admin_name'] . '空单激活'); // 会员激活日志
                money($user_id, $level['amount'], 0, 0,'管理员' . $_SESSION['admin_name'] . '空单激活'); // 增加投资记录
                $this->success("操作成功");
            } else {
                $this->error("操作失败");
            }
        }
    }

    // 设为实单
    public function reall_user() {
        $res = M('users')->where('user_id=' . $id)->save(array('is_user' => 1));
        if ($res) {
            $this->success("取消成功");
        } else {
            $this->error("取消失败");
        }
    }

    /**
     * 空单实单操作结束 ===============================================华丽的分界线
     */

    /**
     * 激活操作开始 ===============================================华丽的分界线
     */
    // 免单激活会员
    public function Free_user() {
        $user_id = I('user_id', '');
        if ($user_id == '') {
            die('<script>alert("操作失败，请刷新页面后重试");history.go(-1);</script>');
        } else {
            $res = M('users')->where('user_id=' . $user_id)->save(array('is_activate' => 1, 'jh_time' => time()));
            if ($res) {
                activate_log($user_id, 1, '管理员' . $_SESSION['admin_name'] . '免单激活'); // 会员激活日志
                $this->success("激活成功");
            } else {
                $this->error("操作失败");
            }
        }
    }

    // 实单激活会员
    public function real_user() {
        $user_id = I('user_id', '');
        if ($user_id == '') {
            die('<script>alert("操作失败，请刷新页面后重试");history.go(-1);</script>');
        } else {
            $user = M('users')->where(array('user_id' => $user_id))->field('user_id,nickname,level,user_money,zuce_money,first_leader,zmd_id')->find();
            $level = M('user_level')->where(array('level_id' => $user['level']))->find(); //会员等级查询
            
            /*$user_money = $level['amount'] * tpCache('reg.kt_2_1') / 100;
            $zuce_money = $level['amount'] * tpCache('reg.kt_2_2') / 100;*/
            $user_money = $level['amount'];
            $zuce_money =0;
            //$zuce_money = $level['amount'] * tpCache('reg.kt_2_2') / 100;
            /*if ($user['user_money'] < $user_money || $user['zuce_money'] < $zuce_money) {
                die('<script>alert("余额不足,请充值后重试");history.go(-1);</script>');
            }*/
            if ($user['user_money'] < $user_money ) {
                die('<script>alert("余额不足,请充值后重试");history.go(-1);</script>');
            }
            $res = M('users')->where('user_id=' . I('user_id'))->save(array('is_activate' => 1, 'jh_time' => time()));
            if ($res) {
                activate_log($user_id, 2, '管理员' . $_SESSION['admin_name'] . '实单激活'); // 会员激活日志
                //M('users')->where(array('user_id' => $user_id))->setInc('total_amount', $level['amount']); // 累积消费

                bochu_day_log($level['amount'], 0);   // 拨出统计
                money($user_id, $level['amount'], 0, 1, '管理员' . $_SESSION['admin_name'] . '实单激活投资'); // 增加投资记录

                accountLog($user_id, '-' . $user_money, '-' . $zuce_money, 0, 0, 0, '+' . $level['amount'], '管理员' . $_SESSION['admin_name'] . '实单激活');
                clear_tjj($user['first_leader'], $level['amount'], $user['nickname'] . '激活'); // 计算分享积分   传推荐人id
                clear_tjj($user['second_leader'], $level['amount'], $user['nickname'] . '激活'); // 计算分享积分   传推荐人id
                clear_tjj($user['third_leader'], $level['amount'], $user['nickname'] . '激活'); // 计算分享积分   传推荐人id
                //clear_bdj($user['zmd_id'], $level['amount'], $user['nickname'] . '激活'); // 计算报单奖   传专卖店id
                //clear_jdj($user['first_leader'], $level['amount'],$user['nickname'] . '激活');// 计算见点奖   传推荐人id
                $this->success("激活成功");
            } else {
                $this->error("操作失败");
            }
        }
    }

    /**
     * 激活操作结束 ===============================================华丽的分界线
     */
    // 实单激活会员 更改为传参数形式
    public function real_user2($user_id,$user_money) {
        if ($user_id == '') {
            die('<script>alert("操作失败，请刷新页面后重试");history.go(-1);</script>');
        } else {
            $user = M('users')->where(array('user_id' => $user_id))->field('user_id,nickname,level,user_money,zuce_money,first_leader,zmd_id')->find();
            $level = M('user_level')->where(array('level_id' => $user['level']))->find(); //会员等级查询

            $zuce_money =0;
            
            if ($user['user_money'] < $user_money ) {
                die('<script>alert("余额不足,请充值后重试");history.go(-1);</script>');
            }
           
                //M('users')->where(array('user_id' => $user_id))->setInc('total_amount', $level['amount']); // 累积消费
                bochu_day_log($user_money, 0);   // 拨出统计
                money($user_id, $user_money, 0, 1, '管理员' . $_SESSION['admin_name'] . '实单激活投资'); // 增加投资记录
                accountLog($user_id, '-' . $user_money, '-' . $zuce_money, 0, 0, 0, '+' . $user_money, '管理员' . $_SESSION['admin_name'] . '实单激活');
                clear_tjj($user['first_leader'], $user_money, $user['nickname'] . '激活',0); // 计算分享积分   传推荐人id
                //clear_bdj($user['zmd_id'], $level['amount'], $user['nickname'] . '激活'); // 计算报单奖   传专卖店id
                //clear_jdj($user['first_leader'], $level['amount'],$user['nickname'] . '激活');// 计算见点奖   传推荐人id
                return true;
            
        }
    }



    /*增加报单*/
    public function addbd() {
        clear_tjj(31,3860,'dddd',0);
        exit('dsfsds');

        $user_id = I('user_id', '');
        if(IS_POST){
         if ($user_id == '') {
            die('<script>alert("操作失败，请刷新页面后重试");history.go(-1);</script>');
            } else {
                $user_money = I('bd_price', '');    
               $res=$this->real_user2($user_id,$user_money);
                if ($res) {
                    $this->success("增加报单成功");
                } else {
                    $this->error("增加报单失败");
                }
            }
        }else{
             $this->assign('user_id', $user_id);
            $this->display();
        }
        
    }


}
